﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 采购计划详情
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CaiGouPlanDetailsController : ControllerBase
    {
        private readonly AppDbContext _context;

        public CaiGouPlanDetailsController(AppDbContext context)
        {
            _context = context;
        }

        // GET: api/CaiGouPlanDetails/GetPlanDetailList
        [Route("GetPlanDetailList")]
        [HttpPost]
        public async Task<ActionResult<IEnumerable<PlanDetail>>> GetPlanDetails(Guid id)
        {
            return await _context.PlanDetail.Include(p=>p.CommodityInformationSheet).Where(p => p.CaigouPlanId == id).ToListAsync();
        }
       

        // GET: api/CaiGouPlanDetails/5
        [HttpGet("{id}")]
        public async Task<ActionResult<PlanDetail>> GetPlanDetail(Guid id)
        {
            var planDetail = await _context.PlanDetail.FindAsync(id);

            if (planDetail == null)
            {
                return NotFound();
            }

            return planDetail;
        }

        // PUT: api/CaiGouPlanDetails/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutPlanDetail(Guid id, PlanDetail planDetail)
        {
            if (id != planDetail.Id)
            {
                return BadRequest();
            }

            _context.Entry(planDetail).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlanDetailExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/CaiGouPlanDetails
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<PlanDetail>> PostPlanDetail(PlanDetail planDetail)
        {
            _context.PlanDetail.Add(planDetail);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetPlanDetail", new { id = planDetail.Id }, planDetail);
        }

        // DELETE: api/CaiGouPlanDetails/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeletePlanDetail(Guid id)
        {
            var planDetail = await _context.PlanDetail.FindAsync(id);
            if (planDetail == null)
            {
                return NotFound();
            }

            _context.PlanDetail.Remove(planDetail);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool PlanDetailExists(Guid id)
        {
            return _context.PlanDetail.Any(e => e.Id == id);
        }
    }
}
